package cn.com.dingtek.genius.manager.server.service;

import cn.com.dingtek.genius.manager.server.repository.db.entity.AppVersion;
import cn.com.dingtek.genius.manager.server.repository.db.manager.AppVersionManager;
import cn.com.dingtek.genius.manager.server.support.OsType;
import cn.com.dingtek.genius.manager.server.utils.MybatisPlusUtil;
import cn.com.dingtek.genius.manager.server.utils.Verify;
import cn.com.dingtek.genius.manager.server.utils.VerifyException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AppVersionService {
    @Autowired
    private AppVersionManager appVersionManager;

    public JSONObject list(AppVersion param, int currentPage, int pageSize) {
        QueryWrapper<AppVersion> query = Wrappers.emptyWrapper();
        if (param != null) {
            query = MybatisPlusUtil.customEqQuery(null, "os_type", param.getOsType());
            query = MybatisPlusUtil.customLikeQuery(query, "version_name", param.getVersionName());
        }

        IPage<AppVersion> userBasePage = appVersionManager.page(new Page<AppVersion>(currentPage, pageSize).setDesc("id"), query);

        List<JSONObject> records = userBasePage.getRecords().stream()
                .map(item -> {
                    JSONObject jsonObject = (JSONObject) JSON.toJSON(item);
                    return jsonObject.fluentPut("osTypeDesc", OsType.valueOf(item.getOsType()).get().getDesc());
                })
                .collect(Collectors.toList());

        return new JSONObject().fluentPut("total", userBasePage.getTotal())
                .fluentPut("records", records);
    }


    public void add(AppVersion param, Date now) {
        try {
            Verify.isTrue(appVersionManager.save(AppVersion.builder()
                    .versionName(param.getVersionName())
                    .versionCode(param.getVersionCode())
                    .filePath(param.getFilePath())
                    .osType(param.getOsType())
                    .signPwd(param.getSignPwd())
                    .project(param.getProject())
                    .remark(param.getRemark())
                    .ctime(now)
                    .version(0)
                    .build()), "新增版本失败.");
        } catch (DuplicateKeyException e) {
            throw new VerifyException("该版本已存在");
        }
    }

    public AppVersion info(Long id) {
        Verify.isTrue(id != null && id > 0, "版本不存在");
        return appVersionManager.getById(id);
    }

    public void update(AppVersion param, Date now) {
        Verify.isTrue(param.getId() != null && param.getId() > 0, "版本不存在");

        AppVersion appVersion = appVersionManager.getById(param.getId());
        Verify.notNull(appVersion, "版本不存在");

        AppVersion toUpdate = AppVersion.builder()
                .id(param.getId())
                .versionName(param.getVersionName())
                .versionCode(param.getVersionCode())
                .filePath(param.getFilePath())
                .osType(param.getOsType())
                .signPwd(param.getSignPwd())
                .project(param.getProject())
                .remark(param.getRemark())
                .version(appVersion.getVersion())
                .build();

        Verify.isTrue(appVersionManager.updateById(toUpdate), "更新版本失败");
    }

    public void remove(Long id) {
        if (id != null && id > 0) {
            appVersionManager.removeById(id);
        }
    }

}
